<?php

/**
 * barrio actions.
 *
 * @package    sf_sandbox
 * @subpackage barrio
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class barrioActions extends sfActions
{
    
    public function executeShow(sfWebRequest $request)
  {
    $this->barrio = Doctrine_Core::getTable('Barrio')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->barrio);
  }
  public function executeIndex(sfWebRequest $request)
  {
     $this->barrios = $this->getPager(BarrioTable::getBuscarBarrios(""), $request->getGetParameter('page'), "Barrio");
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new BarrioForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BarrioForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($barrio = Doctrine_Core::getTable('Barrio')->find(array($request->getParameter('id'))), sprintf('Object barrio does not exist (%s).', $request->getParameter('id')));
    $this->form = new BarrioForm($barrio);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($barrio = Doctrine_Core::getTable('Barrio')->find(array($request->getParameter('id'))), sprintf('Object barrio does not exist (%s).', $request->getParameter('id')));
    $this->form = new BarrioForm($barrio);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($barrio = Doctrine_Core::getTable('Barrio')->find(array($request->getParameter('id'))), sprintf('Object barrio does not exist (%s).', $request->getParameter('id')));
    $barrio->delete();

    $this->redirect('barrio/index');
  }
  public function executeBuscarZona(sfWebRequest $request) {
        $this->setLayout("busqueda");
    }
    public function executeEliminar(sfWebRequest $request) {
           $this->forward404Unless($this->barrio = Doctrine_Core::getTable('Barrio')->find(array($request->getParameter('id'))), sprintf('Object barrio does not exist (%s).', $request->getParameter('id')));
                         
    }
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $barrio = $form->save();

      $this->redirect('barrio/index');
    }
  }
   protected function getPager($q, $page, $model) {
        $pager = new sfDoctrinePager($model, sfConfig::get('app_nro_x_pagina_contribuyente'));
        $pager->setQuery($q);
        $pager->setPage($page);
        $pager->init();

        return $pager;
    }
  public function executeBuscarBarrios(sfWebRequest $request) {
        if ($request->isMethod('post')) {
            $nombre = $request->getPostParameter("nombre");
           } else {
            $nombre = $request->getGetParameter("nombre");
          }
        $this->barrios = $this->getPager(BarrioTable::getBuscarBarrios($nombre), $request->getGetParameter('page'), "Barrio");
        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial("barrio/listadobarrios", array('barrios' => $this->barrios));
        }
    }
     public function executeFiltrarZona(sfWebRequest $request){
    
        if ($request->isMethod('post')) {
            $dato = $request->getPostParameter("dato");
        } else {
            $dato = $request->getGetParameter("dato");
        }
        $this->zona = $this->getPager(ZonaTable::getBuscarZonas($dato), $request->getGetParameter('page'), "Zona");
        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial("barrio/resultadoZonas", array('zona' => $this->zona));
        }
    }
     
}
